package com.jrx.anytxn.transaction.job.cmpsCrt.config;

import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.entity.BtGuaranteeList;
import com.jrx.anytxn.transaction.job.autoPayment.bean.PaymentLogBean;
import com.jrx.anytxn.transaction.job.cmpsCrt.step.CmpsCrtPaymentPreWriter;
import com.jrx.anytxn.transaction.job.cmpsCrt.step.CmpsPaymentPreProcessor;
import com.jrx.anytxn.transaction.job.cmpsCrt.step.CmpsPaymentPreReader;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisPagingItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;


/**
 * 代偿扣款准备
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class CmpsPaymentPreConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;
    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;
    @Autowired
    private CmpsPaymentPreProcessor cmpsPaymentPreProcessor;
    @Autowired
    private CmpsCrtPaymentPreWriter cmpsCrtPaymentPreWriter;
    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Bean
    public Job cmpsPaymentPreJob() {
        return jobBuilderFactory.get("cmpsPaymentPreJob")
                .start(cmpsPaymentPreStep())
                .build();
    }

    @Bean
    public Step cmpsPaymentPreStep(){
        return stepBuilderFactory.get("cmpsPaymentPreStep")
                .<BtGuaranteeList, PaymentLogBean>chunk(anytxnBatchProperties.getCmpsPaymentPreJobConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(cmpsPaymentPreReader())
                .processor(cmpsPaymentPreProcessor)
                .writer(cmpsCrtPaymentPreWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getCmpsPaymentPreJobConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public MyBatisPagingItemReader<BtGuaranteeList> cmpsPaymentPreReader() {
        return new CmpsPaymentPreReader(sqlSessionFactory, anytxnBatchProperties.getCmpsPaymentPreJobConfig().getPageSize());
    }
}
